package org.zxyj.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Properties资源文件操作工具类
 * 
 * @author liaochente
 * 
 */
public class PropertiesUtil {

    /**
     * 资源对象
     */
    private Properties prop = new Properties();

    /**
     * 构造方法
     * 
     * @param ins
     * @throws IOException
     */
    private PropertiesUtil(InputStream ins) throws IOException {
        prop.load(ins);
    }

    /**
     * 生成工具类对象
     * 
     * @param path
     *            资源文件路径
     * @return
     * @throws RuntimeException
     */
    public static PropertiesUtil newInstance(String path) {
        FileInputStream fis = null;
        PropertiesUtil util = null;
        try {
            if (path.indexOf("%") >= 0) {
                path = URLDecoder.decode(path, "utf-8");// 处理Tomcat路径中空格问题
            }
            fis = new FileInputStream(new File(path));
            util = new PropertiesUtil(fis);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return util;
    }

    /**
     * 获取资源文件的值
     * 
     * @param key
     * @return
     */
    public String getValue(String key) {
        return prop.getProperty(key);
    }
}
